%GLOBAL CWD; %LET CWD = %SUBSTR(%SYSGET(SAS_EXECFILEPATH), 1, %EVAL(%LENGTH(%SYSGET(SAS_EXECFILEPATH)) - %LENGTH(%SYSGET(SAS_EXECFILENAME)) - 1));

/* EXECUTE ALL FILES IN THE CURRENT DIRECTORY WHOSE NAME MATCHES A SPECIFIED PATTERN */
%MACRO RUNALL;
	FILENAME DIR "&CWD.";

	DATA DLISTING (KEEP = FILENAME);
		HANDLE = DOPEN("DIR");
		IF HANDLE > 0 THEN DO;
			COUNT = DNUM(HANDLE);
			DO INDX = 1 TO COUNT;
				FILENAME = UPCASE(TRIM(DREAD(HANDLE, INDX)));								
				OUTPUT DLISTING;
			END;
		END;			
		RC = DCLOSE(HANDLE);		
	RUN;

	DATA DLISTING; SET DLISTING;		
		/* SUBSET FILES USING NAMING RULES */		
		/* <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< */
		IF INDEX(FILENAME, "ITEM_") = 1;
		/* >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> */
		
	RUN;

	/* GET NUMBER OF FILES */
	%LET FNUM = 0;
	DATA _NULL_; SET DLISTING;		
		CALL SYMPUT("FNUM", _N_);		
	RUN;

	/* FILE NAMES WILL BE IN ASCENDING ALPHABETICAL ORDER */	

	%DO RUNREPS_INDX = 1 %TO &FNUM;
		DATA _NULL_; SET DLISTING;
			IF _N_ EQ &RUNREPS_INDX;
			CALL SYMPUT("FIN", FILENAME);
		RUN;		
		/* DO SOMETHING WITH EACH FILE. */
		/* <<<<<<<<<<<<<<<<<<<<<<<<<<<< */
		%INCLUDE "&CWD\&FIN";
		/* >>>>>>>>>>>>>>>>>>>>>>>>>>>> */
	%END;	
%MEND;
%RUNALL;


